package com.cisdi.blog.web.admin;

import com.cisdi.blog.entity.Blog;
import com.cisdi.blog.entity.Record;
import com.cisdi.blog.service.RecordService;
import com.cisdi.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

/**
 * @author pengyangyan
 * @version 1.0.0
 * @date 2020年04月29日 10:33:00
 */
@Controller
@RequestMapping("/admin/records")
public class RecordController {

    @Autowired
    RecordService recordService;

    @GetMapping
    public String records(@PageableDefault(size = 10,sort = {"createTime"},direction = Sort.Direction.DESC) Pageable pageable,
                        Model model){
        model.addAttribute("page",recordService.pageRecords(pageable));
        return "admin/records";
    }

    @GetMapping("/input")
    public String tagInput(Model model){
        model.addAttribute("record",new Record());
        return "admin/records_input";
    }

    @GetMapping("/{id}/input")
    public String tagInput(@PathVariable("id") Long id,Model model){
        model.addAttribute("record",recordService.getRecord(id));
        return "admin/records_input";
    }

    @PostMapping("/post")
    public String addTag(@Valid Record record,RedirectAttributes attributes){
        if (record.getId()==null){
            Record addRecord = recordService.addRecord(record);
            if (addRecord==null){
                attributes.addFlashAttribute("message","新增失败");
                attributes.addFlashAttribute("status",false);
            }else {
                attributes.addFlashAttribute("message","新增成功");
                attributes.addFlashAttribute("status",true);
            }
        }else {
            Record updateRecord = recordService.updateRecord(record);
            if (updateRecord==null){
                attributes.addFlashAttribute("message","更新失败");
                attributes.addFlashAttribute("status",false);
            }else {
                attributes.addFlashAttribute("message","更新成功");
                attributes.addFlashAttribute("status",true);
            }
        }

        return "redirect:/admin/records";

    }

    @GetMapping("/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        recordService.deleteRecord(id);
        attributes.addFlashAttribute("message","删除成功");
        attributes.addFlashAttribute("status",true);
        return "redirect:/admin/records";
    }
}
